/**
 * @author Gautham Gajula
 *	
 *
 * getHint(this, event,0,3, 'waynfx.html?wci=GetLocationHint&search_type=with_city_key&town=Y&where=')
 */
var selBGColor = '#E1F5C0';
var unselBGColor = '#FFFFFF';
var selFGColor = '#3E3F41';
var unselFGColor = '#3E3F41';
var isDOM = $ //DOM1 browser 
var isO   = isO5 = window.opera && isDOM; //Opera 5+
var isIE  = document.all && document.all.item && !isO

var fnListOnMouseover 	= new Function();
var fnListOnMouseout  	= new Function();
var fnListOnMousedown 	= new Function();
var fnOnHintEnter 		= new Function();	
var fnOnHintTab 		= new Function();
var fnOnHintDown 		= new Function();
var fnOnHintUp 			= new Function();

var iCount = 0;
var bPreSelectFirst = true;

function getHint(obj, event, startChr,rangeChr , url){
	
	
	obj.setAttribute('blockResponse','0');
	if(!event) var event = window.event;
	var objName = obj.id;
	
	var xmlRequest = null;

	var cur_div = obj.getAttribute('cur_div');
	var delKey = false;
	//var chr = '';
	var div_list = $(objName+'_o');



	// div list style
	obj.setAttribute("maxlength",255);	
	obj.setAttribute("autocomplete",'off');
	//if ( div_list.style.width == null || div_list.style.width.stripAllWhite() == "" ) {
		//div_list.style.width = obj.style.width;
	//}
	
	
	if(event.keyCode == 8 || event.keyCode == 46) delKey = true;
	//if(!delKey) chr = String.fromCharCode(event.keyCode).toLowerCase();
	
	
	
	switch(event.keyCode){

		
		case 40://down			
	
			if(div_list.childNodes.length == 0) return true;			
			if ((isIE && typeof cur_div == 'number' && cur_div >=0) || (!isIE && cur_div !='') || (isIE && cur_div !='')){			
				div_list.childNodes[cur_div].style.background = unselBGColor;
				div_list.childNodes[cur_div].style.color = unselFGColor;
				++cur_div;	
			}

			if(cur_div == '' || cur_div == null) {obj.setAttribute('cur_div',0); cur_div=0;}
			if( cur_div > div_list.childNodes.length - 1 ) cur_div = 0;			
			//$('var1').value = cur_div;
			
			div_list.childNodes[cur_div].style.background = selBGColor;
			div_list.childNodes[cur_div].style.color = selFGColor;
			obj.setAttribute('cur_div',cur_div);
			
			//alert("down"+cur_div);
			
			try{
				fnOnHintDown(obj);
			}catch(e){}
			return;
			break;
		case 38://up
			if(isNaN(cur_div) || cur_div == null || cur_div=='') {obj.setAttribute('cur_div',0); cur_div=0;}
			if(div_list.childNodes.length==0) return true;
			div_list.childNodes[cur_div].style.background=unselBGColor;
			div_list.childNodes[cur_div--].style.color=unselFGColor;			
			if(cur_div<0) cur_div = div_list.childNodes.length - 1;
			//$('var1').value = cur_div;
			div_list.childNodes[cur_div].style.background=selBGColor;
			div_list.childNodes[cur_div].style.color=selFGColor;
			obj.setAttribute('cur_div',cur_div);
			try{
				fnOnHintUp(obj);
			}catch(e){}
			
			return;
			break;
	}
	
	// do nothing
	if(event.keyCode==27 || event.keyCode==16  || event.keyCode==35 || event.keyCode==36 || event.keyCode==37 || event.keyCode==39) return true;


	// get where letters
	var strTmp = (obj.value);
	
	
	var where = '';	
	if (strTmp.length > startChr) where = strTmp.substr(startChr,strTmp.length);
	 
	var allowGetXml = false;
	if (strTmp.length >= startChr + rangeChr) {
		allowGetXml = true;	
	}else{
		hideBoxHint(obj);
	}
	
	

	if((delKey && allowGetXml) || (!delKey && allowGetXml)){		
		try{			
			
			
			var projectName = document.getElementById("projectName").value;
			var image = "";
			if(image != "")
				MM_setTextOfLayer(image,'','&nbsp;<img src="'+projectName+'/images/arrow.gif" alt="arrow" height="14px">') 
		
		    var req = init();
		    req.onreadystatechange = processRequest;
		    req.open("GET", url, false); 
	     	req.send(url);
			  
		    function init() {
		        if (window.XMLHttpRequest) {
		        return new XMLHttpRequest();
		      } else if (window.ActiveXObject) {
		        return new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    } 
		    
		    function processRequest () {
		   		
		      if (req.readyState == 4) {
		        if (req.status == 200) {
			      
			      		///////////////////////
			      		
			      	var docElement = req.responseXML;
					var list = docElement.getElementsByTagName('Item');
						
					var divContent = '';
					
					for(i = 0;i < list.length;i++){
						
						var id = list[i].getElementsByTagName("Id")[0].firstChild.data;
						var name = list[i].getElementsByTagName("Name")[0].firstChild.data;
						
						divContent += "<div input_name='"+ obj.id +"' a='"+id+"' onmouseover='fnHMouseOver(this,"+i+")' onmousedown='fnHMouseClick(this,"+i+");' onmouseout='fnHMouseOut(this,"+i+")'>" + name +"</div>";
						
					}
			      
						
					div_list.innerHTML = divContent;	
							if(divContent != "") {							
								div_list.style.display = "block";
								try{WCH.Apply(div_list);}catch(err){}
								$(obj.id+'_key').value = 0;									
							}else{
								$(obj.id+'_key').value = 0;		
								hideBoxHint(obj);
							}
							obj.setAttribute('cur_div','');
														
							//$('var1').value = '';
							xmlRequest = null;
		        }
		        else if (req.status == 400) {
		        	//if(image != "")
					//	MM_setTextOfLayer(image,'','')
		        	//alert("Please write proper Url for "+document.getElementById(list).title);
		        	//return false;
		        }
		      }
		    }
		    
		    
		this.doGet = function() {
	      req.open("GET", url, true); 
	      req.send(url);
	    }
	    
	    this.doSynchronousGet = function() {
	      req.open("GET", url, false); 
	      req.send(url);
	    }
	    
	    this.doPost = function(body) {
	      req.open("POST", url, true);
	      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	      req.send(body);
	    }
			
			
		}catch(err){
			alert('smartxml:'+err);
		}		
  	}else{
		if((delKey && where.length==1) || (!delKey && where.length==0)) {			
			hideBoxHint(div_list.id,obj);
		}
	}
}

	function fnHMouseOut(div, cur_div){
		var obj = $(div.getAttribute('input_name'));
		div.style.background = unselBGColor;
		div.style.color = unselFGColor;
		obj.setAttribute('cur_div','');
		//$('var1').value = '';
		try{
			fnListOnMouseout(obj);
		}catch(e){}
	}
	function fnHMouseOver(div, cur_div){		
		var obj = $(div.getAttribute('input_name'));
		if (obj.getAttribute('cur_div') != null && !isNaN(obj.getAttribute('cur_div')) && obj.getAttribute('cur_div') != ''){
			div.parentNode.childNodes[obj.getAttribute('cur_div')].style.background = unselBGColor;
			div.parentNode.childNodes[obj.getAttribute('cur_div')].style.color = unselFGColor;			
		}	
		div.style.background = selBGColor;
		div.style.color = selFGColor;
		obj.setAttribute('cur_div',cur_div);
		//alert(div.style.background);
		//$('var1').value = cur_div;
		//var div_list = div.parentNode;
		
		//$(div.getAttribute('input_name')+'_key').value = div_list.childNodes[cur_div].getAttribute('a').replace(/&amp;/gi,"&");		
		//obj.value = div_list.childNodes[cur_div].innerHTML.replace(/&amp;/gi,"&");		
		obj.value = div.getAttribute('a');;
		
		try{
			fnListOnMouseover(obj);	
		}catch(e){}
	}

	function fnHMouseClick(div, cur_div){
		
		
		//var div_list = div.parentNode;
	 	var obj = $(div.getAttribute('input_name'));	
		obj.value = div.getAttribute('a');
		
		hideBoxHint(obj);
		//$('var1').value = cur_div;
		try{
			fnListOnMousedown(obj);	
		}catch(e){}	
		


	}
	function hideBoxHint(obj){
		try {
			obj.setAttribute('blockResponse','1');
		} catch(e){}  	
	var divLName = obj.id+'_o';	
		if ($(divLName) != null){
			divL = $(divLName);
			divL.style.display = 'none';
			try{
				WCH.Discard(divL);
			}catch(err){}
			divL.innerHTML = '';
			//obj.setAttribute('cur_div','');	
		}
	}

	function fnHSelectFirst(div, cur_div){		
		var obj = $(div.getAttribute('input_name'));
	
		if (obj.getAttribute('cur_div') != null && !isNaN(obj.getAttribute('cur_div')) && obj.getAttribute('cur_div') != ''){
			div.parentNode.childNodes[obj.getAttribute('cur_div')].style.background = unselBGColor;
			div.parentNode.childNodes[obj.getAttribute('cur_div')].style.color = unselFGColor;			
		}	
		div.style.background = selBGColor;
		div.style.color = selFGColor;
		obj.setAttribute('cur_div',cur_div);

		var div_list = div.parentNode;
		$(div.getAttribute('input_name')+'_key').value = div_list.childNodes[cur_div].getAttribute('a').replace(/&amp;/gi,"&");		
		
		try{
			fnListOnMouseover(obj);	
		}catch(e){}
	}
	
	function $() {
	var elements=new Array();
	for (var i=0;i < arguments.length;i++) {
	var element=arguments[i];
	if (typeof element=='string')
	element=document.getElementById(element);
	if (arguments.length==1)
	return element;
	elements.push(element);
	}
	return elements;
	}
